<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 标题 -->
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!-- 内容 -->
      <el-tree
        :data="list"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
      >
        <template v-slot="scope">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="" /><span>{{ scope.data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ scope.data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="doAdd(scope.data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="doEdit(scope.data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="doDelete(scope.data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 弹窗 -->
      <el-dialog
        :visible.sync="isShow"
        :title="edit ? '编辑'+'部门':'添加'+'部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="doReset"
      >
        <departmentDialog
          ref="refDialog"
          :pid="curId"
          :edit="edit"
          :origin-list="originList"
          @success="success"
          @close="doClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteDepartment } from '@/api/departments'
import departmentDialog from './departmentDialog.vue'
import { arr2tree } from '@/utils/index'
export default {
  components: { departmentDialog },
  data() {
    return {
      // 部门信息
      list: [{
        'id': '1',
        'pid': '',
        name: '财务部'
      },
      {
        'id': '2',
        'pid': '1',
        name: '财务核算部'
      },
      {
        'id': '3',
        'pid': '1',
        name: '税务核算部'
      }
      ],
      // 控制弹窗
      isShow: false,
      // 点击部门的id
      curId: '',
      // 如果点击新增按钮，则修改值为false，如果点击编辑按钮，则修改值为true
      edit: false,
      originList: [{
        id: '',
        pid: '',
        name: ''
      }]
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 1.发送ajax拿到部门信息，并渲染页面
    async loadDepartments() {
      const res = await getDepartments()
      res.data.depts.shift()
      // const res1 = arr2tree(this.list, '')
      // console.log(res1)
      this.originList = res.data.depts
      this.list = arr2tree(res.data.depts, '')
    },
    // 点2.击添加部门按钮，弹窗出现
    doAdd(id) {
      this.isShow = true
      this.edit = false
      this.curId = id
    },
    // 3.子传父，修改部门成功后触发函数
    success() {
      this.isShow = false
      this.loadDepartments()
    },
    // 4.删除部门
    doDelete(id) {
      this.$confirm('是否删除部门', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.getDelete(id)
      }).catch(() => {
      })
    },
    // 5.删除部门函数
    async getDelete(id) {
      await deleteDepartment(id)
      this.loadDepartments()
    },
    // 6.编辑部门
    async doEdit(id) {
      this.edit = true
      this.curId = id
      this.isShow = true
      /* 解决bug:在子组件的created函数里面，调用getDepartment函数。当组件在第一次打开时，会走created钩子函数，第二次打开不会调用，因为关闭组件，组件只被隐藏。
      第一种办法： 给子组件添加v-if，当值为true时，组件加载，但是当值为false时，组件会被销毁，所以每次打开组件，组件都会重新渲染，会走一次子组件的created钩子
      第二种办法：在父组件里面，点击编辑按钮时，就调用getDepartment函数。
      第三种办法：在子组件里面，添加watch，检测pid的值变化，则调用getDepartment函数(见子组件58行)
        */
      this.$nextTick(() => {
        this.$refs.refDialog.getDepartment()
      })
    },
    // 7.取消
    doClose() {
      this.isShow = false
    },
    // 8.当弹框关闭时，调用的函数
    doReset() {
      this.$refs.refDialog.reset()
    }
  }
}
</script>

<style>

</style>
